<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
        <a-col :span="24">
          <a-radio-group name="radioGroup" :default-value="1" @change="onChange">
            <a-radio :value="1">
              添加章
            </a-radio>
            <a-radio :value="2">
              添加节
            </a-radio>
          </a-radio-group>
         </a-col>
          <a-col :span="24" v-if='sectionRedio==1'>
            <a-form-model-item label="章名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sectionName">
              <a-input v-model="model.sectionName" placeholder="请输入章名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if='sectionRedio==2'>
            <a-form-model-item label="节名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sectionName">
              <a-input v-model="model.sectionName" placeholder="请输入节名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if='sectionRedio==2'>
            <a-form-model-item label="本节简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sectionAbout">
              <a-input v-model="model.sectionAbout" placeholder="请输入本节简介"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if='sectionRedio==1'>
            <a-form-model-item label="本章简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sectionAbout">
              <a-input v-model="model.sectionAbout" placeholder="请输入本章简介"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属课程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stageId">
              <a-input v-model="model.courseId" placeholder="请输入所属课程"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if='sectionRedio==2'>
            <a-form-model-item label="所属章" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
              <a-input v-model="model.pid" placeholder="请输入本节所属章id"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if='sectionRedio==2'>
            <a-form-model-item label="本节课程地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入本节的视频所在网址"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CourseSectionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        sectionRedio:1,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/cousesection/courseSection/add",
          edit: "/cousesection/courseSection/edit",
          queryById: "/cousesection/courseSection/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      onChange(e) {
        if ( e.target.value==1){
          this.sectionRedio=1
        }
        if ( e.target.value==2){
          this.sectionRedio=2
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>